using System;
using System.Collections.Generic;
using System.Text;
using Wrath.Xml;
using Wrath.RuleForge.Interfaces;
using System.ComponentModel;

namespace Wrath.RuleForge.Property
{
    public class FrameDefWrapper : IPropertyWrapper<Element>
    {
        private bool hasChanged;
        private Element element;

        private Wrath.Xml.Attribute nameAttribute;
        private Wrath.Xml.Attribute bitmapAttribute;
        private Wrath.Xml.Attribute topLeft;
        private Wrath.Xml.Attribute top;
        private Wrath.Xml.Attribute topRight;
        private Wrath.Xml.Attribute left;
        private Wrath.Xml.Attribute middle;
        private Wrath.Xml.Attribute right;
        private Wrath.Xml.Attribute bottomLeft;
        private Wrath.Xml.Attribute bottom;
        private Wrath.Xml.Attribute bottomRight;
        private Wrath.Xml.Attribute decal;

        [Category("Frame Definition")]
        [Description("The name of this Frame.")]
        public string Name { get { return nameAttribute.Value; } set { nameAttribute.Value = value; } }

        [Category("Frame Definition")]
        [Description("The bitmap of this Frame.")]
        public string Bitmap { get { return bitmapAttribute.Value; } set { bitmapAttribute.Value = value; } }

        [Category("Frame Rectangles")]
        [Description("The top left rectangle.")]
        [DisplayName("Top Left")]
        public string TopLeft 
        { 
            get 
            {
                if (topLeft == null) return "";
                return topLeft.Value; 
            } 
            set 
            {
                if (topLeft != null)
                    topLeft.Value = value;
                else
                {
                    topLeft = AddRectangle("topleft", value);
                }
            } 
        }

        [Category("Frame Rectangles")]
        [Description("The top rectangle.")]
        [DisplayName("Top")]
        public string Top
        {
            get
            {
                if (top == null) return "";
                return top.Value;
            }
            set
            {
                if (top != null)
                    top.Value = value;
                else
                {
                    top = AddRectangle("top", value);
                }
            }
        }

        [Category("Frame Rectangles")]
        [Description("The top right rectangle.")]
        [DisplayName("Top Right")]
        public string TopRight
        {
            get
            {
                if (topRight == null) return "";
                return topRight.Value;
            }
            set
            {
                if (topRight != null)
                    topRight.Value = value;
                else
                {
                    topRight = AddRectangle("topright", value);
                }
            }
        }

        [Category("Frame Rectangles")]
        [Description("The left rectangle.")]
        [DisplayName("Left")]
        public string Left
        {
            get
            {
                if (left == null) return "";
                return left.Value;
            }
            set
            {
                if (left != null)
                    left.Value = value;
                else
                {
                    left = AddRectangle("left", value);
                }
            }
        }

        [Category("Frame Rectangles")]
        [Description("The middle rectangle.")]
        [DisplayName("Middle")]
        public string Middle
        {
            get
            {
                if (middle == null) return "";
                return middle.Value;
            }
            set
            {
                if (middle != null)
                    middle.Value = value;
                else
                {
                    middle = AddRectangle("middle", value);
                }
            }
        }

        [Category("Frame Rectangles")]
        [Description("The right rectangle.")]
        [DisplayName("Right")]
        public string Right
        {
            get
            {
                if (right == null) return "";
                return right.Value;
            }
            set
            {
                if (right != null)
                    right.Value = value;
                else
                {
                    right = AddRectangle("right", value);
                }
            }
        }

        [Category("Frame Rectangles")]
        [Description("The bottom left rectangle.")]
        [DisplayName("Bottom Left")]
        public string BottomLeft
        {
            get
            {
                if (bottomLeft == null) return "";
                return bottomLeft.Value;
            }
            set
            {
                if (bottomLeft != null)
                    bottomLeft.Value = value;
                else
                {
                    bottomLeft = AddRectangle("bottomleft", value);
                }
            }
        }

        [Category("Frame Rectangles")]
        [Description("The top left rectangle.")]
        [DisplayName("Bottom")]
        public string Bottom
        {
            get
            {
                if (bottom == null) return "";
                return bottom.Value;
            }
            set
            {
                if (bottom != null)
                    bottom.Value = value;
                else
                {
                    bottom = AddRectangle("bottom", value);
                }
            }
        }

        [Category("Frame Rectangles")]
        [Description("The top left rectangle.")]
        [DisplayName("Bottom Right")]
        public string BottomRight
        {
            get
            {
                if (bottomRight == null) return "";
                return bottomRight.Value;
            }
            set
            {
                if (bottomRight != null)
                    bottomRight.Value = value;
                else
                {
                    bottomRight = AddRectangle("bottomright", value);
                }
            }
        }

        [Category("Frame Rectangles")]
        [Description("The rectangle that defines the decal.")]
        [DisplayName("Decal")]
        public string Decal
        {
            get
            {
                if (decal == null) return "";
                return decal.Value;
            }
            set
            {
                if (decal != null)
                    decal.Value = value;
                else
                {
                    decal = AddRectangle("decal", value);
                }
            }
        }


        public FrameDefWrapper(Element frameDef)
        {
            this.element = frameDef;

            nameAttribute = frameDef.FindAttribute("name");

            Element element = frameDef.FindFirstElement("bitmap", false);
            if (element != null)
            {
                bitmapAttribute = element.FindAttribute("file");
            }

            element = frameDef.FindFirstElement("topleft", false);
            if (element != null)
            {
                topLeft = element.FindAttribute("rect");                
            }

            element = frameDef.FindFirstElement("top", false);
            if (element != null)
            {
                top = element.FindAttribute("rect");
            }

            element = frameDef.FindFirstElement("topright", false);
            if (element != null)
            {
                topRight = element.FindAttribute("rect");
            }

            element = frameDef.FindFirstElement("left", false);
            if (element != null)
            {
                left = element.FindAttribute("rect");
            }

            element = frameDef.FindFirstElement("middle", false);
            if (element != null)
            {
                middle = element.FindAttribute("rect");
            }

            element = frameDef.FindFirstElement("right", false);
            if (element != null)
            {
                right = element.FindAttribute("rect");
            }

            element = frameDef.FindFirstElement("bottomleft", false);
            if (element != null)
            {
                bottomLeft = element.FindAttribute("rect");
            }

            element = frameDef.FindFirstElement("bottom", false);
            if (element != null)
            {
                bottom = element.FindAttribute("rect");
            }

            element = frameDef.FindFirstElement("bottomRight", false);
            if (element != null)
            {
                bottomRight = element.FindAttribute("rect");
            }

            element = frameDef.FindFirstElement("decal", false);
            if (element != null)
            {
                decal = element.FindAttribute("rect");
            }


        }

        private Wrath.Xml.Attribute AddRectangle(string name, string value)
        {
            Element rectElement = new Element();
            rectElement.Name = name;

            Wrath.Xml.Attribute rect = new Wrath.Xml.Attribute();
            rect.Name = "rect";
            rect.Value = value;

            rectElement.Attributes.Add(rect);

            element.ChildElements.Add(rectElement);

            return rect;
        }

        #region IPropertyWrapper<Element> Members

        [Browsable(false)]
        public bool HasChanged
        {
            get
            {
                return hasChanged;
            }
            set
            {
                hasChanged = value;
            }
        }

        [Browsable(false)]
        public Element Data
        {
            get { return element; }
        }

        #endregion
    }
}
